require([
  'jquery',
  'pageInit',
  'dataTableFilter',
  'loading',
  'bootstrap-dialog',
  'notification',
  'listAsyncOp'
], function($, vamakerPage, VamDataTable, VamLoader, BootstrapDialog, VamOpMsg, VamListOp) {
  $(function() {
    var 
      $dataTable = $('#data-table'),
      dataUrl= VAM.getVar('STRATEGY_DATA_LIST'),
      dataAuditOp = VAM.getVar('LIST_AUDIT_OP_URL'),
      dataLinkUrl = VAM.getVar('LINK_URL'),
      // dataPauseOp = VAM.getVar('LIST_PAUSE_OP_URL'),
      // dataResumeOp = VAM.getVar('LIST_RESUME_OP_URL'),
      $reportDataThead = $dataTable.find('thead'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {};
    // Table settings.
    tableOptions = {
      'ajax': {
        'url': dataUrl
      },
      'order': [8,'desc'],
      'columns': [
        {'data': 'id'},
        {'data': 'name'},
        {'data': 'launch'},
        {'data': 'status'},
        {'data': 'editStatus'},
        {'data': 'totalBudget'},
        {'data': 'oldBudget'},
        {'data': 'launchTime'},
        {'data': 'modifiedDate'},
        {'data': 'operate'}
      ],
      'columnDefs': [
        {
          'targets': 0,
          'render': function (data, type, full, meta) {
            var ckbText = '<label><input class="ckb-children" type="checkbox" value="' + data + '"/></label>';
            ckbText = '';
            return '<p title="' + data + '">' + ckbText + data +'</p>';
          }
        },
        {
          'targets': 3,
          'render': function (data, type, full, meta) {
            var result = '';
            if (data === 'archived') {
              result = VAM.j('Archived');
            } else if (data === 'effective') {
              result = VAM.j('Effective');
            } else if (data === 'waiting') {
              result = VAM.j('Waiting');
            } else if (data === 'paused') {
              result = VAM.j('Paused');
            } else if (data === 'closed') {
              result = VAM.j('Closed');
            } else if (data === 'incomplete') {
              result = VAM.j('Incomplete');
            };
            return '<p title="' + result + '">' + result +'</p>';
          }
        },
        {
          'targets': [2,4,5,6,7,8],
          'render': function (data, type, full, meta) {
            return '<p title="' + data + '">' + data +'</p>'
          }
        },
        {
          'targets': 1,
          'render': function (data, type, full, meta) {
            var result;
            if (full.creativeCount === 0) {
              return '<p title="' + data + '">' + data +'</p>';
            };
            result = '<p title="' + data + '"><a href="' + dataLinkUrl + '?solution_id=' + full.id + '" target="_blank">' + data + '</a></p>';
            return result;
          }
        },
        {
          'targets': -1,
          'render': function (data, type, full, meta) {
            return '<div class="tab-btn-group">'
                  + '<input type="hidden" class="row-info" value="' + full.id + '"/>'
                  + '<a href="javascript:void(0);" class="btn btn-primary btn-xs tab-audit" data-type="audit" title="' + VAM.j('Not audit') + '"><i class="fa fa-check"></i></a>'
                  + '</div>';
          }
        }
      ],
      'initComplete': function() {
        tableResize = new VamDataTable().columnResize($dataTable);
      }
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });

    $dataTable.on('click', 'tbody td .tab-audit', function (evt) {
      var $me = $(this),
        $rowInfo = $me.siblings('.row-info'),
        opId = $rowInfo.val(),
        opType = $me.data('type'),
        opData;
      evt.preventDefault();
      opData = {
        solution_id: opId,
        type: opType,
        rnd: (new Date()).getTime()
      };
      BootstrapDialog.confirm({
        title: VAM.j('Solution Audit'),
        message: VAM.j('Confirm pass this solution?'),
        type: BootstrapDialog.TYPE_PRIMARY,
        closable: true, 
        draggable: true,
        btnCancelLabel: VAM.j('Cancel'), 
        btnOKLabel: VAM.j('OK'), 
        btnOKClass: 'btn-primary', 
        callback: function(result) {
          if(result) {
            VamListOp.listOp({
              url: dataAuditOp,
              data: opData,
              before: function () {
                $me.button('loading');
              },
              success: function (data) {
                if (data.status === 'success') {
                  VamOpMsg.messager(data);
                  listTable.ajax.reload();
                } else {
                  VamOpMsg.messager(data);
                }
              },
              always: function () {
                $me.button('reset');
              }
            });
          }
        }
      });
    });
  });
});
